﻿using Ch9_Portable.Models;
using Ch9_Portable.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch9_Portable.ViewModels
{
    public class SectionViewModelBase : INotifyPropertyChanged
    {
        public SectionViewModelBase() { }

        #region SeriesItems (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _seriesItems = new ObservableCollection<Ch9Item>();
        public ObservableCollection<Ch9Item> SeriesItems
        {
            get { return _seriesItems; }
            set
            {
                _seriesItems = value;
                NotifyPropertyChanged("SeriesItems");
            }
        }
        #endregion

        #region SelectedItem (Ch9Item)
        private Ch9Item _selectedItem = null;
        public Ch9Item SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyPropertyChanged("SelectedItem");
            }
        }
        #endregion

        #region SelectedSeries (Ch9Item)
        private Ch9Item _selectedSeries = null;
        public Ch9Item SelectedSeries
        {
            get { return _selectedSeries; }
            set
            {
                _selectedSeries = value;
                NotifyPropertyChanged("SelectedSeries");
            }
        }
        #endregion

        public async Task LoadSelectedSeries(Ch9Item series){
            SelectedSeries = series;
            var seriesPosts = await Ch9RssService.GetSeriesAsync(SelectedSeries.feed);
            SeriesItems.Clear();
            foreach (Ch9Item ch9 in seriesPosts.channel.items)
                SeriesItems.Add(ch9);
        }

        public void ClearSelectedSeries()
        {
            SeriesItems.Clear();
            SelectedSeries = null;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }        
    }
}
